package com.lingkun.demo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingkun.demo.sys.entity.RoleMenu;
import com.lingkun.demo.sys.entity.SysRole;
import com.lingkun.demo.sys.entity.sysVo.RoleVo;
import com.lingkun.demo.sys.service.RoleMenuService;
import com.lingkun.demo.sys.service.SysMenuService;
import com.lingkun.demo.sys.service.SysRoleService;
import com.lingkun.demo.utils.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小龙
 * @since 2021-04-24
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private  SysRoleService sysRoleService;
    @Autowired
    private RoleMenuService roleMenuService;


    @GetMapping("/search/{mnName}")
    @ApiOperation(value = "获取可以访问改路由的角色id")
    public DataResult getOperateMenuRoleIds(@PathVariable("mnName") String mnName){
        List<String> idList = sysMenuService.searchMenuRoleIds(mnName);
        return DataResult.getResult(20000,"success",idList);
    }

    @GetMapping("/search1")
    @ApiOperation(value = "获取所有角色的集合")
    public DataResult search( ){
        List<RoleVo> roleList = sysRoleService.search();
        return DataResult.getResult(20000,"success",roleList);
    }

    @PutMapping("/update/{roleId}")
    @ApiOperation(value = "角色的基本信息,分配角色的路由")
    // 角色的基本信息， 分配角色的路由
    public DataResult updateRoleMenu(@PathVariable("roleId") String roleId, @RequestBody RoleVo roleVo){
        Boolean flag =  roleMenuService.updateRoleMenu(roleId,roleVo);
        return flag?DataResult.getResult(20000,"success"):DataResult.getResult(20001,"错误");
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建角色及其分配角色的权限")
    public DataResult addRole(@RequestBody RoleVo roleVo){
        Boolean flag =  roleMenuService.addRole(roleVo);
        return flag?DataResult.getResult(20000,"success"):DataResult.getResult(20001,"cuow");
    }

    @DeleteMapping("/delete/{roleId}")
    @ApiOperation(value = "删除角色及其角色的权限")
    @Transactional
    public DataResult delete(@PathVariable("roleId") String roleId){
        QueryWrapper<SysRole>  queryWrapper =  new QueryWrapper<>();
            queryWrapper.eq("role_id",roleId);
            sysRoleService.remove(queryWrapper);
            Boolean flag =   roleMenuService.removeRoleMenu(roleId);
        return flag?DataResult.getResult(20000,"success"):DataResult.getResult(20001,"删除失败");
    }

}

